package com.zby.admin.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zby.admin.pojo.Category;
import com.zby.admin.pojo.dto.CategoryDto;
import com.zby.admin.service.CategoryService;
import com.zby.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /**
     * 获取所有分类
     */
    @GetMapping("/getAllCategory")
    public Result<Page<Category> > getAllCategory( @RequestParam(defaultValue = "1") Integer page,
                                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("获取所有分类");
        log.info("分页参数：{}",page,pageSize);
        // 调用Service获取分页数据
        Page<Category> pageResult = categoryService.selectList(page, pageSize);

       if (pageResult != null){
           return Result.success(pageResult);
       }
      return Result.error(500,"获取分类失败");
    }
    /**
     * 上传分类
     */
    @PostMapping("/uploadCategory")
    public Result<CategoryDto> uploadCategory(@RequestBody CategoryDto categoryDto) {
        log.info("上传分类");
        log.info("上传分类参数：{}",categoryDto);
      categoryService.insert(categoryDto);
        return Result.success();
    }
    /**
     * 删除分类
     */
    @DeleteMapping("/deleteCategory")
    public Result<Integer> deleteCategory(@RequestParam Integer id) {
        boolean b = categoryService.removeById(id);
        if (b){
            return Result.success();
        }
        return Result.error(500,"删除分类失败");
    }
    /**
     * 修改分类
     */
    @PutMapping("/updateCategory")
    public Result<CategoryDto> updateCategory(@RequestBody CategoryDto categoryDto) {
        boolean update = categoryService.update(categoryDto);
        if (update){
            return Result.success();
        }
        return Result.error(500,"修改分类失败");
    }
}
